/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.ipartek.formacion.gestformwww.controlador;

import java.io.IOException;
import java.util.ResourceBundle;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Aratz Setién
 */
public class AccionRouter {
    private String clave=null;
    private  boolean esReenvio=true;
    public AccionRouter (String key)
    {
        this (key,true);

    }
    public AccionRouter(String key, boolean isForward)
    {
        this.esReenvio=isForward;
        this.clave=key;
    }
    //Este método es llamado desde el servlet de acciones
    public  synchronized void enruta (HttpServlet servlet,HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException
    {
        ResourceBundle bundle=(ResourceBundle) servlet.getServletContext().getAttribute("vistas-mapeo");
        //ResourceBundle bundle=(ResourceBundle) servlet.getServletContext().getAttribute("acciones-mapeo");
        String url=bundle.getString(clave);
        if (esReenvio)
        {
            RequestDispatcher rd=request.getRequestDispatcher(response.encodeURL(url));
            if (rd != null) {
                rd.forward(request, response);
            }
        }
        else
        {
            response.sendRedirect(response.encodeURL(url));
        }


    }

}
